package cn.tedu.live8.security;

import cn.tedu.live8.mapper.LoginMapper;
import cn.tedu.live8.pojo.vo.LoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserDetailsServeImpl implements UserDetailsService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.debug("自动根据用户名{}查询用户详情",username);

        LoginVO loginVO = loginMapper.getByUsername(username);
        log.debug("数据库中查找的数据是:{}",loginVO);
        if(loginVO!=null){
            UserDetails userDetails = User.builder()
                    .username(loginVO.getUsername())
                    .password(loginVO.getPassword())
                    .disabled(false)
                    .accountLocked(false)
                    .accountExpired(false)
                    .credentialsExpired(false)
                    .authorities("临时的权限")
                    .build();
            log.debug("UserDetails数据是{}",userDetails);
            log.debug("UserDetails类型是:{}",userDetails.getClass().getName());
            return userDetails;
        }

        log.debug("该用户名没有对应的数据,用户不存在");
        return null;
    }
}
